//
//  NSData+Extension.m
//  zdOrderPlatform
//
//  Created by dfwx on 2020/8/26.
//  Copyright © 2020 zhongdaodianzi. All rights reserved.
//

#import "NSData+Extension.h"



@implementation NSData (Extension)

/**
*  data转16进制
*/
- (NSString *)convertDataToHexStr {
    if (!self || [self length] == 0) {
            return @"";
        }
        NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[self length]];
        [self enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
            unsigned char *dataBytes = (unsigned char*)bytes;
            for (NSInteger i = 0; i < byteRange.length; i++) {
                NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
                if ([hexStr length] == 2) {
                    [string appendString:hexStr];
                } else {
                    [string appendFormat:@"0%@", hexStr];
                }
            }
        }];
        return string;
}
@end
